package com.talkweb.historylib.utils;

import com.talkweb.securitycamera.common.interfaces.entity.IBaseFileEntity;
import com.talkweb.securitycamera.utils.DensityUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author SUQI
 * @date 2021/2/4
 * @description
 **/
public class ImageItemHelper {
    public static int COLUMN_NUM = 3;
    private final static int MARGIN_VP_16 = 16;

    /**
     * 计算imageView的宽
     *
     * @param padding
     * @return
     */
    public static int getImageWidth(int padding) {
        int startAndEndMargin = DensityUtils.vp2px(MARGIN_VP_16);
        //（屏幕宽度-component之间padding-前后margin）/ item个数
        return (DensityUtils.SCREEN_WIDTH_PIXELS - ((ImageItemHelper.COLUMN_NUM - 1) * padding) - startAndEndMargin * 2) / ImageItemHelper.COLUMN_NUM;
    }

    /**
     * 格式化数据源
     *
     * @return
     */
    public static List<List<IBaseFileEntity>> formatList(List<IBaseFileEntity> list) {
        if (list == null) {
            return null;
        }
        int listSize = list.size();
        int rowCount = list.size() % COLUMN_NUM == 0 ? list.size() / COLUMN_NUM : (list.size() / COLUMN_NUM) + 1;
        int index = 0;

        List<List<IBaseFileEntity>> resultList = new ArrayList<>();
        for (int i = 0; i < rowCount; i++) {
            List<IBaseFileEntity> itemList = new ArrayList<>();
            for (int j = 0; j < COLUMN_NUM; j++) {
                if (index < listSize) {
                    itemList.add(list.get(index));
                    index++;
                }
            }
            resultList.add(itemList);
        }
        return resultList;
    }
}
